package com.sy.live.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class FileUtil
{
	private static String message;

	/**
	 * 新建目录
	 * 
	 * @param folderPath
	 *            目录
	 * @return 返回目录创建后的路径
	 */
	public static String createFolder(String folderPath)
	{
		String txt = folderPath;
		try
		{
			java.io.File myFilePath = new java.io.File(txt);
			txt = folderPath;
			if (!myFilePath.exists())
			{
				myFilePath.mkdirs();
			}
		} catch (Exception e)
		{
			message = "创建目录操作出错";
		}
		return txt;
	}

	/**
	 * 新建文件
	 * 
	 * @param filePathAndName
	 *            文本文件完整绝对路径及文件名
	 * @param fileContent
	 *            文本文件内容
	 * @return
	 */
	public static void createFile(String filePathAndName, String fileContent)
	{

		try
		{
			String filePath = filePathAndName;
			filePath = filePath.toString();
			File myFilePath = new File(filePath);
			if (!myFilePath.exists())
			{
				myFilePath.createNewFile();
			}
			FileWriter resultFile = new FileWriter(myFilePath);
			PrintWriter myFile = new PrintWriter(resultFile);
			String strContent = fileContent;
			myFile.println(strContent);
			myFile.close();
			resultFile.close();
		} catch (Exception e)
		{
			message = "创建文件操作出错";
		}
	}

	/**
	 * 删除文件
	 * 
	 * @param filePathAndName
	 *            文本文件完整绝对路径及文件名
	 * @return Boolean 成功删除返回true遭遇异常返回false
	 */
	public static void delFile(String pathandname)
	{
		File file = new File(pathandname);
		if (file.exists() && file.isFile())
			file.delete();
	}

	/**
	 * 删除文件夹
	 * 
	 * @param folderPath
	 *            文件夹完整绝对路径
	 * @return
	 */
	public static void delFolder(String folderPath)
	{
		try
		{
			delAllFile(folderPath); // 删除完里面所有内容
			String filePath = folderPath;
			filePath = filePath.toString();
			java.io.File myFilePath = new java.io.File(filePath);
			myFilePath.delete(); // 删除空文件夹
		} catch (Exception e)
		{
			message = ("删除文件夹操作出错");
		}
	}

	/**
	 * 删除指定文件夹下所有文件
	 * 
	 * @param path
	 *            文件夹完整绝对路径
	 * @return
	 * @return
	 */
	public static boolean delAllFile(String path)
	{
		boolean bea = false;
		File file = new File(path);
		if (!file.exists())
		{
			return bea;
		}
		if (!file.isDirectory())
		{
			return bea;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++)
		{
			if (path.endsWith(File.separator))
			{
				temp = new File(path + tempList[i]);
			} else
			{
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile())
			{
				temp.delete();
			}
			if (temp.isDirectory())
			{
				delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
				delFolder(path + "/" + tempList[i]);// 再删除空文件夹
				bea = true;
			}
		}
		return bea;
	}

	/**
	 * 复制单个文件
	 * 
	 * @param oldPathFile
	 *            准备复制的文件源
	 * @param newPathFile
	 *            拷贝到新绝对路径带文件名
	 * @return
	 */
	public static void copyFile(String oldPathFile, String newPathFile)
	{
		try
		{
			int byteread = 0;
			File oldfile = new File(oldPathFile);
			if (oldfile.exists())
			{ // 文件存在时
				InputStream inStream = new FileInputStream(oldPathFile); // 读入原文件
				FileOutputStream fs = new FileOutputStream(newPathFile);
				byte[] buffer = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1)
				{
					// System.out.println(bytesum);
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
			}
		} catch (Exception e)
		{
			message = ("复制单个文件操作出错");
		}
	}

	/**
	 * 复制整个文件夹的内容
	 * 
	 * @param oldPath
	 *            准备拷贝的目录
	 * @param newPath
	 *            指定绝对路径的新目录
	 * @return
	 */
	public static void copyFolder(String oldPath, String newPath)
	{
		try
		{
			new File(newPath).mkdirs(); // 如果文件夹不存在 则建立新文件夹
			File a = new File(oldPath);
			String[] file = a.list();
			File temp = null;
			for (int i = 0; i < file.length; i++)
			{
				if (oldPath.endsWith(File.separator))
				{
					temp = new File(oldPath + file[i]);
				} else
				{
					temp = new File(oldPath + File.separator + file[i]);
				}
				if (temp.isFile())
				{
					FileInputStream input = new FileInputStream(temp);
					FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString());
					byte[] b = new byte[1024 * 5];
					int len;
					while ((len = input.read(b)) != -1)
					{
						output.write(b, 0, len);
					}
					output.flush();
					output.close();
					input.close();
				}
				if (temp.isDirectory())
				{// 如果是子文件夹
					copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
				}
			}
		} catch (Exception e)
		{
			message = "复制整个文件夹内容操作出错";
		}
	}

	/**
	 * 移动文件
	 * 
	 * @param oldPath
	 * @param newPath
	 * @return
	 */
	public static void moveFile(String oldPath, String newPath)
	{
		File descFile = new File(newPath);
		if (!descFile.getParentFile().exists())
		{
			createFolderCascade(descFile.getParent());
			// System.out.println(descFile.getParent());
		}
		copyFile(oldPath, newPath);
		delFile(oldPath);
	}

	/**
	 * 移动目录
	 * 
	 * @param oldPath
	 * @param newPath
	 * @return
	 */
	public static void moveFolder(String oldPath, String newPath)
	{
		copyFolder(oldPath, newPath);
		delFolder(oldPath);
	}

	/**
	 * 获取出错信息
	 * 
	 * @return
	 */
	public static String getMessage()
	{
		return message;
	}

	/**
	 * 取出文件列表:取回指定文件夹中的文件
	 * 
	 * @param path
	 *            文件目录名
	 * @return 文件全路径列表
	 * @throws IOException
	 */
	public static String[] getFileList(String path, String ext)
	{
		if (!path.endsWith(File.separator))
			path += File.separator;
		File file = new File(path);
		List<String> resList = new ArrayList<String>();
		try
		{
			if (!file.exists())
			{
				throw new IOException("folder doesn't exist!!");
			}
			if (!file.isDirectory())
			{
				throw new IOException("this isn't a folder");
			}
			String fileList[] = file.list();
			for (String fileitem : fileList)
			{
				if (fileitem.endsWith(ext))
				{ // can't
					resList.add(path + fileitem);
				}
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return resList.toArray(new String[0]);
	}

	/**
	 * 级联建立新目录
	 * 
	 * @param path
	 */
	public static void createFolderCascade(String path)
	{
		File dir = new File(path);
		if (dir.getParent() != null)
		{
			if (!dir.getParentFile().exists())
				createFolderCascade(dir.getParent());
			createFolder(path);
		}
	}

	/**
	 * 获取磁盘驱动器空闲空间
	 * 
	 * @param driver
	 * @return
	 */
	public static long getFreeSpace(String driver)
	{
		File file = new File(driver);
		return file.getFreeSpace();
	}

	/**
	 * 获取磁盘驱动器未使用空间
	 * 
	 * @param driver
	 * @return
	 */
	public static long getUsedSpace(String driver)
	{
		return getTotalSpace(driver) - getFreeSpace(driver);
	}

	/**
	 * 获取磁盘驱动器总空间
	 * 
	 * @param driver
	 * @return
	 */
	public static long getTotalSpace(String driver)
	{
		File file = new File(driver);
		return file.getTotalSpace();
	}

	/**
	 * 尝试建立新目录
	 * 
	 * @param Path
	 */
	public static void tryCreateFolderCascade(String Path)
	{
		File folder = new File(Path);
		if (!folder.exists())
		{
			createFolderCascade(Path);
		}
	}

	/**
	 * get file path
	 * 
	 * @param nameandpath
	 * @return
	 */
	public static String getFilePath(String nameandpath)
	{
		File file = new File(nameandpath);
		return file.getParent();
	}

	/**
	 * get file name
	 * 
	 * @param nameandpath
	 * @return
	 */
	public static String getFileName(String nameandpath)
	{
		File file = new File(nameandpath);
		return file.getName();
	}

	/**
	 * 向文件里写内容
	 * 
	 * @param content
	 * @param writeFile
	 */
	public static void writeFileContent(String content, File writeFile)
	{
		try
		{
			FileWriter writer = new FileWriter(writeFile, true);
			writer.append(content + "\n");
			writer.close();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public static void zipFile()
	{

		try
		{
			ZipInputStream inputStream = new ZipInputStream(new FileInputStream("D:\\MakeRing"));

			ZipEntry zEntry = null;

			while ((zEntry = inputStream.getNextEntry()) != null)
			{
				System.out.println(zEntry.getName());
			}

			inputStream.close();
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args)
	{
		zipFile();
	}
}
